package com.longcai.piaojia.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.Conn;
import com.longcai.piaojia.conn.GetBalanceGet;
import com.longcai.piaojia.conn.LatestBalancePayPost;
import com.longcai.piaojia.util.WxPay;
import com.module.zfb.PayAction;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2017/1/11.
 */
public class LatestPayActivity extends BaseActivity implements View.OnClickListener{

    public static final String PAY_RECEIVER = "payBroadcastReceiver";
    public static final String PAY_FINISH_RECEIVER = "payBroadcastFinishReceiver";

    @BoundView(R.id.title_back_rl) private RelativeLayout title_back_rl;

    @BoundView(R.id.pay_price_rl) private RelativeLayout pay_price_rl;
    @BoundView(R.id.pay_type_ll) private LinearLayout pay_type_ll;
    @BoundView(R.id.pay_price_tv) private TextView pay_price_tv;

    @BoundView(R.id.pay_price_check_tv) private TextView pay_price_check_tv;
//    @BoundView(R.id.pay_price_check_rl) private RelativeLayout pay_price_check_rl;
//    @BoundView(R.id.pay_price_check_sel_iv) private ImageView pay_price_check_sel_iv;
//    @BoundView(R.id.pay_price_check_no_iv) private ImageView pay_price_check_no_iv;

    @BoundView(R.id.pay_wechat_rl) private RelativeLayout pay_wechat_rl;
    @BoundView(R.id.pay_wechat_check_sel_iv) private ImageView pay_wechat_check_sel_iv;
    @BoundView(R.id.pay_wechat_check_no_iv) private ImageView pay_wechat_check_no_iv;
    @BoundView(R.id.pay_alipay_rl) private RelativeLayout pay_alipay_rl;
    @BoundView(R.id.pay_alipay_check_sel_iv) private ImageView pay_alipay_check_sel_iv;
    @BoundView(R.id.pay_alipay_check_no_iv) private ImageView pay_alipay_check_no_iv;
    @BoundView(R.id.activity_pay_price_tv) private TextView activity_pay_price_tv;
    @BoundView(R.id.pay_submit_bt) private TextView pay_submit_bt;

    private GetBalanceGet.BalanceEntity balance;

    private int payType = 0;
    private String orderNum;
    private String truePrice;
    private String title;


    private GetBalanceGet getBalanceGet = new GetBalanceGet(new AsyCallBack<GetBalanceGet.BalanceEntity>(){

        @Override
        public void onSuccess(String toast, int type, GetBalanceGet.BalanceEntity balanceEntity) throws Exception {
            super.onSuccess(toast, type, balanceEntity);

            balance = balanceEntity;

            //如果余额小于 支付总价 显示支付宝和微信支付
            if (new BigDecimal(balance.wallet).subtract(new BigDecimal(truePrice)).signum() != -1) {

                pay_price_rl.setVisibility(View.VISIBLE);
                pay_type_ll.setVisibility(View.GONE);

                pay_price_check_tv.setText("-" + getResources().getString(R.string.money_icon_text) + truePrice + getResources().getString(R.string.goods_info_money_text));

                payType = 2;

            } else {

                pay_price_rl.setVisibility(View.GONE);
                pay_type_ll.setVisibility(View.VISIBLE);

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(LatestPayActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });



    private LatestBalancePayPost latestBalancePayPost = new LatestBalancePayPost(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            payEnd();

            Toast.makeText(LatestPayActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            payFinish();

            Toast.makeText(LatestPayActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_latest_pay);

        title_back_rl.setVisibility(View.VISIBLE);

        setTitleName(getResources().getString(R.string.pay_title_text));

        truePrice = getIntent().getExtras().getString("truePrice");
        orderNum = getIntent().getExtras().getString("orderNum");
        title = getIntent().getExtras().getString("title", "友票—活动预定");

        activity_pay_price_tv.setText(truePrice);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PAY_RECEIVER);
        intentFilter.addAction(PAY_FINISH_RECEIVER);

        registerReceiver(payBroadCast, intentFilter);

        title_back_rl.setOnClickListener(this);
        pay_wechat_rl.setOnClickListener(this);
        pay_alipay_rl.setOnClickListener(this);
        pay_submit_bt.setOnClickListener(this);

        getBalanceGet.user_id = BaseApplication.BasePreferences.readUserId();
        getBalanceGet.execute(this);

    }

    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.title_back_rl:

                payFinish();

                break;

            case R.id.pay_price_check_rl:


                break;

            case R.id.pay_wechat_rl:

                pay_wechat_check_sel_iv.setVisibility(View.VISIBLE);
                pay_alipay_check_no_iv.setVisibility(View.VISIBLE);

                pay_wechat_check_no_iv.setVisibility(View.GONE);
                pay_alipay_check_sel_iv.setVisibility(View.GONE);

                payType = 0;

                break;

            case R.id.pay_alipay_rl:

                pay_alipay_check_sel_iv.setVisibility(View.VISIBLE);
                pay_wechat_check_no_iv.setVisibility(View.VISIBLE);

                pay_alipay_check_no_iv.setVisibility(View.GONE);
                pay_wechat_check_sel_iv.setVisibility(View.GONE);

                payType = 1;

                break;

            case R.id.pay_submit_bt:

                switch (payType) {

                    case 0:

                        WxPay wxPay = new WxPay(LatestPayActivity.this);

                        wxPay.pay(title, orderNum, String.valueOf(Float.parseFloat(truePrice) * 100).replace(".0", ""), "3");
//                        wxPay.pay(title, orderNum, "1", "3");

                        break;

                    case 1:

                        PayAction payAction = new PayAction(LatestPayActivity.this) {
                            @Override
                            protected void onSuccess() {

                                payEnd();

                            }

                            @Override
                            protected void onEnd() {

                                payFinish();

                            }
                        };

                        try {

                            payAction.pay(title, "这是详情", orderNum, truePrice, Conn.SERVICE + Conn.ALIPAY_ACTIVITY_NOTIFYURL);
//                            payAction.pay(title, "这是详情", orderNum, "0.01", Conn.SERVICE + Conn.ALIPAY_ACTIVITY_NOTIFYURL);


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        break;

                    case 2:

                        latestBalancePayPost.user_id = BaseApplication.BasePreferences.readUserId();
                        latestBalancePayPost.amount = truePrice;
                        latestBalancePayPost.order_number = orderNum;

                        latestBalancePayPost.execute(this);

                        break;

                }

                pay_submit_bt.setEnabled(false);


                break;

        }

    }

    private void payFinish(){

        finish();

    }

    private void payEnd(){

        startActivity(new Intent(LatestPayActivity.this, MyAppointmentActivity.class));

        finish();

    }

    public BroadcastReceiver payBroadCast = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            if(intent.getAction().equals(PAY_RECEIVER)){

                payEnd();

            } else if(intent.getAction().equals(PAY_FINISH_RECEIVER)){

                payFinish();

            }

        }

    };


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            payFinish();

            return false;
        }


        return super.onKeyDown(keyCode, event);
    }

}
